<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <!-- 搜索区域 -->
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="4" :sm="5">
            <a-form-item label="任务名称" :labelCol="{ span: 5 }" :wrapperCol="{ span: 18, offset: 1 }">
              <j-input placeholder="请输入任务名称" v-model="queryParam.writeName"></j-input>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="5">
            <a-form-item label="任务描述" :labelCol="{ span: 5 }" :wrapperCol="{ span: 18, offset: 1 }">
              <j-input placeholder="请输入任务描述" v-model="queryParam.writeDesc"></j-input>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="5">
            <a-form-item label="固化类型" :labelCol="{ span: 5 }" :wrapperCol="{ span: 18, offset: 1 }">
              <j-dict-select-tag v-model="queryParam.writeType" dictCode="write_type" placeholder="请选择固化类型" />
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="5">
            <a-form-item label="目标模型" :labelCol="{ span: 5 }" :wrapperCol="{ span: 18, offset: 1 }">
              <j-search-select-tag
                placeholder="请选择目标模型"
                v-model="queryParam.topicId"
                dict="sc_situation_index_topic,topic_name,id"
                :async="true"
              >
              </j-search-select-tag>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="10">
            <a-button type="primary" icon="search" @click="searchQuery">查询</a-button>
            <a-button type="primary" icon="reload" style="margin-left: 8px" @click="searchReset">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="handleAddModal">新增</a-button>
      <a-button type="primary" icon="delete" @click="batchDel">批量删除</a-button>
      <j-super-query :fieldList="superQueryFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery" />
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i>
        已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>
        项
        <a style="margin-left: 24px" @click="onClearSelected">取消勾选</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        @change="handleTableChange"
      >
        <span slot="status_dictText" slot-scope="status_dictText">
          <span :style="{ color: status_dictText === '启用' ? 'green' : 'red' }">{{ status_dictText }}</span>
        </span>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEditRecord(record.topicId)">编辑</a>
          <a-divider type="vertical" />

          <a-popconfirm title="确定立即执行吗?" @confirm="() => handleExecuteNow(record)">
            <a>立即执行</a>
          </a-popconfirm>

          <a-divider type="vertical" />

          <a-popconfirm placement="topLeft" title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>

        </span>
      </a-table>
    </div>
    <TopicWriteModal
      ref="addWriteModal"
      v-if="addFlag"
      :modalVisible="addFlag"
      pageFlag="write"
      :currentId="currentKey"
      @close="handleClose"
    />
  </a-card>
</template>

<script>
import JSuperQuery from '@/components/jeecg/JSuperQuery'
import { getAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import TopicWriteModal from './modules/TopicWriteModal'
import JInput from '@/components/jeecg/JInput.vue'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'

export default {
  name: 'ScSituationIndexTopicWriteList',
  components: {
    JSuperQuery,
    TopicWriteModal,
    JInput,
    JSearchSelectTag
  },
  mixins: [JeecgListMixin],
  data() {
    return {
      // 表头
      columns: [
        {
          title: '任务名称',
          align: 'center',
          dataIndex: 'writeName'
        },
        {
          title: '任务描述',
          align: 'center',
          dataIndex: 'writeDesc'
        },
        {
          title: '任务类型',
          align: 'center',
          dataIndex: 'writeType_dictText'
        },
        {
          title: '任务时间',
          align: 'center',
          dataIndex: 'writeDate'
        },
        {
          title: '是否重试',
          align: 'center',
          dataIndex: 'needRetry_dictText'
        },
        {
          title: '重试次数',
          align: 'center',
          dataIndex: 'retryTimes'
        },
        {
          title: '间隔时间',
          align: 'center',
          dataIndex: 'retryInterval'
        },
        {
          title: '目标模型',
          align: 'center',
          dataIndex: 'topicId_dictText'
        },
        {
          title: '固化方式',
          align: 'center',
          dataIndex: 'writeFilterType_dictText'
        },
        {
          title: '接口状态',
          align: 'center',
          dataIndex: 'status_dictText',
          scopedSlots: { customRender: 'status_dictText' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 240,
          scopedSlots: { customRender: 'action' }
        }
      ],
      superQueryFieldList: [
        { type: 'datetime', value: 'writeDate', text: '任务时间' },
        { type: 'number', value: 'retryInterval', text: '间隔时间' },
        { type: 'select', value: 'needRetry', text: '是否重试', dictCode: 'need_retry' },
        { type: 'select', value: 'writeType', text: '固化方式', dictCode: 'write_type' },
        { type: 'select', value: 'status', text: '接口状态', dictCode: 'status' },
        { type: 'input', value: 'createBy', text: '创建人' },
        { type: 'datetime', value: 'createTime', text: '创建时间' }
      ],
      url: {
        list: '/index/scSituationIndexTopicWrite/list',
        delete: '/index/scSituationIndexTopicWrite/delete',
        deleteBatch: '/index/scSituationIndexTopicWrite/deleteBatch',
        executeNow: '/topic/config/runningOnce'
      },
      addFlag: false,
      currentKey: ''
    }
  },
  created() { },
  methods: {

    /**
     * @description 固化任务立即执行
     * @param {Object} item
     * @returns void
     */
    handleExecuteNow(item) {
      if (item.id !== '') {
        this.loading = true
        getAction(this.url.executeNow, { writeId: item.id }).then((response) => {
          if (response.code === 200) {
            this.$message.success(response.message)
            this.loadData()
          } else {
            this.$message.warning(response.message)
          }
        }).finally(() => {
          this.loading = false
        })
      }
    },

    /**
    * @description 添加modal框关闭
    * @returns void
    */
    handleClose(flag) {
      this.addFlag = false
      if (flag) {
        // 调用查询接口更新查询数据
        this.loadData(1)
      }
    },

    /**
     * @description 添加操作
     * @returns void
     */
    handleAddModal() {
      this.addFlag = true
      this.currentKey = ''
    },

    /**
     * @description 编辑操作
     * @param {*} id
     * @returns void
     */
    handleEditRecord(id) {
      this.addFlag = true
      this.currentKey = id
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
<style lang="less">
</style>
